package com.icebergteam.warfacewidget.ui.configuration;

import java.util.ArrayList;
import com.icebergteam.warfacewidget.WidgetProvider;
import com.icebergteam.warfacewidget.ui.AdBannerFragment;
import com.icebergteam.warfacewidget.R;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class ConfigurationActivity extends FragmentActivity {
	PrefsGeneralFragment	generalFragment;
	PrefsFinderFragment		finderFragment;
	PrefsNewsFragment		newsFragment;	
	final static String className = ConfigurationActivity.class.getCanonicalName();
	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;
	
	// Animation
	private static Animation tapFieldAnimation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//FIXME:remove it for force localized title activity
		this.setTitle(R.string.prefs_activity_title);
		setContentView(R.layout.activity_configuration);
		
		generalFragment	= new PrefsGeneralFragment();
		//finderFragment	= new PrefsFinderFragment();
		newsFragment	= new PrefsNewsFragment();
		
		// Create the adapter that will return a fragment for each of the three
		// primary sections of the app.
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager());

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.conf_activity_pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		
		AdBannerFragment.setBanner(this, savedInstanceState, R.id.conf_activity_adbanner_layout);
		
		// Prepare animation
		tapFieldAnimation = AnimationUtils.loadAnimation(ConfigurationActivity.this, R.anim.tap_prefs_field);
		tapFieldAnimation.reset();
		// Set current page
		mViewPager.setCurrentItem(getIntent().getIntExtra("FRAGMENT_ID", 0));
	}

	public static void startTapFieldAnimation(View v) {
		v.clearAnimation();
		v.startAnimation(tapFieldAnimation);
	}

	public void onClick(View v) throws Exception	{
		if(generalFragment.checkFields(v))	{
			return;
//		} else if(finderFragment.checkFields(v)) {
//			return;
		} else if(newsFragment.checkFields(v))	{
			return;
		}
	}
	
	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
	 * one of the sections/tabs/pages.
	 */
	public class SectionsPagerAdapter extends FragmentPagerAdapter {
		
		ArrayList <PreferenceFragment> m_prefsArrayList;  

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public android.support.v4.app.Fragment getItem(int position) {
			switch(position)	{
			case PrefsGeneralFragment.FRAGMENT_ID:
				return generalFragment;
//			case PrefsFinderFragment.FRAGMENT_ID:
//				return finderFragment;
			case PrefsNewsFragment.FRAGMENT_ID:
				return newsFragment;
			}
			return null;
			
		}

		@Override
		public int getCount() {
			// Show 3 total pages.
			return 2;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			switch (position) {
			case PrefsGeneralFragment.FRAGMENT_ID:
				return getString(R.string.prefs_title_general);
//			case PrefsFinderFragment.FRAGMENT_ID:
//				return getString(R.string.prefs_title_finder);
			case PrefsNewsFragment.FRAGMENT_ID:
				return getString(R.string.prefs_title_news);
			}
			return null;
		}
	}

	/**
	 * A dummy fragment representing a section of the app, but that simply
	 * displays dummy text.
	 */
	public static class DummySectionFragment extends Fragment {
		/**
		 * The fragment argument representing the section number for this
		 * fragment.
		 */
		public static final String ARG_SECTION_NUMBER = "section_number";

		public DummySectionFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			// Create a new TextView and set its text to the fragment's section
			// number argument value.
			TextView textView = new TextView(getActivity());
			textView.setGravity(Gravity.CENTER);
			textView.setText(Integer.toString(getArguments().getInt(
					ARG_SECTION_NUMBER)));
			return textView;
		}
	}

	@Override
	protected void onDestroy() {
		WidgetProvider.refreshWidget(getIntent());
		super.onDestroy();
	}

}
